package com.ruoyi.web.controller.serve;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.serve.domain.ServerUrl;
import com.ruoyi.serve.service.IServerUrlService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 资源管理Controller
 * 
 * @author ruoyi
 * @date 2023-11-15
 */
@RestController
@RequestMapping("/serve/serveUrl")
public class ServerUrlController extends BaseController
{
    @Autowired
    private IServerUrlService serverUrlService;

    /**
     * 查询资源管理列表
     */
    @PreAuthorize("@ss.hasPermi('serve:serveUrl:list')")
    @GetMapping("/list")
    public TableDataInfo list(ServerUrl serverUrl)
    {
        startPage();
        List<ServerUrl> list = serverUrlService.selectServerUrlList(serverUrl);
        return getDataTable(list);
    }

    /**
     * 导出资源管理列表
     */
    @PreAuthorize("@ss.hasPermi('serve:serveUrl:export')")
    @Log(title = "资源管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ServerUrl serverUrl)
    {
        List<ServerUrl> list = serverUrlService.selectServerUrlList(serverUrl);
        ExcelUtil<ServerUrl> util = new ExcelUtil<ServerUrl>(ServerUrl.class);
        util.exportExcel(response, list, "资源管理数据");
    }

    /**
     * 获取资源管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('serve:serveUrl:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(serverUrlService.selectServerUrlById(id));
    }

    /**
     * 新增资源管理
     */
    @PreAuthorize("@ss.hasPermi('serve:serveUrl:add')")
    @Log(title = "资源管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ServerUrl serverUrl)
    {
        return toAjax(serverUrlService.insertServerUrl(serverUrl));
    }

    /**
     * 修改资源管理
     */
    @PreAuthorize("@ss.hasPermi('serve:serveUrl:edit')")
    @Log(title = "资源管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ServerUrl serverUrl)
    {
        return toAjax(serverUrlService.updateServerUrl(serverUrl));
    }

    /**
     * 删除资源管理
     */
    @PreAuthorize("@ss.hasPermi('serve:serveUrl:remove')")
    @Log(title = "资源管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(serverUrlService.deleteServerUrlByIds(ids));
    }

    /**
     * 统计资源管理
     */
    @GetMapping("/countList")
    public AjaxResult countList()
    {
        List<ServerUrl> list = serverUrlService.selectServerUrlList(null);

        Map<String, Object> map = new HashMap<>();

        List<ServerUrl> abnormalServerUrl = new ArrayList<>();

        // 获取异常的网站
        for (ServerUrl url : list) {
            if (url.getState().equals(1L)){
                abnormalServerUrl.add(url);
            }
        }

        map.put("total", list.size());
        map.put("abnormalTotal", abnormalServerUrl.size());
        map.put("abnormalServerUrl", abnormalServerUrl);

        return success(map);
    }

}
